home *** CD-ROM | disk | FTP | other *** search
- Path: unix.sri.com!usenet
- From: mklenk@updike.sri.com (Mark Klenk)
- Newsgroups: comp.lang.c
- Subject: Re: Returning a variable from system() functi
- Date: 19 Jan 1996 16:01:57 GMT
- Organization: SRI International
- Message-ID: <4dof9l$fk9@unix.sri.com>
- References: <295336694wnr@iiga.demon.co.uk>
- Reply-To: mklenk@updike.sri.com
- NNTP-Posting-Host: 204.75.161.40
-
- Pete Ryan wrote:
- >
- > I`ve just been experimenting with C on UNIX and have come across a
- >problem!. There is a UNIX command called `find / -name gwire -print`
- >which scans the UNIX drive for files/directories containing `gwire`.
- >Anyway what I want to do is the following...
- >
- >#include <stdio.h>
- >#include <stdlib.h>
-
- Add
- #include <unistd.h>
-
-
- >void main()
- >{
- > char tmp[];
- >
- > chdir("/usr2/willesden");
- >
- > */ offending code below ;( */
- > tmp = system("find . -name gwire");
-
- Instead, use popen() - see the man page for popen/pclose.
- FILE * fp;
- char line[1024];
- fp = popen("find . -name gwire", "r");
- if (NULL == fp) {
- fprintf(stderr, "Unable to execute command.\n");
- exit(EXIT_FAILURE);
- }
- while (NULL != fgets(line, sizeof(line), fp)) {
- printf("%s", line);
- }
- pclose(fp);
-
- >Therefore if I run `find . -name gwire -print` it returns all the
- >directories containing `gwire`. However the code above does not
- >work!!. Is it because `= system` returns an integer and not strings???
-
- Yes, that's part of your answer. system() returns the status
- of the command, not its output. The output usually goes to
- stdout and/or stderr, of which you can only capture stdout
- with popen().
-
-
-
- ---
-
- mklenk@coronacorp.com (Mark Klenk)
-
-
-
-